<?php

Loader::using("Models::Entities::Lineas");

/**
 * @export Modules
 * @export-metadata RequiredRoles:Agencia
 */
class ManejarLineas extends Module {

    function index() {
        $lineas = $this->components->Lineas->getAll();

        $this->assign("newURL", $this->route->action("create"));
        $this->assign("lineas", $lineas);

        $this->display("index");
    }
    
    function view($id = null) {
        $id = $id ? $id : $this->request->get("id");
        
        $linea = $this->components->Lineas->getById($id);
        
        $this->assign("linea", $linea);
        
        $this->display("view");
    }

    function edit() {
        $id = $this->request->get("id");
        $linea = $this->components->Lineas->getById($id);

        $this->assign("returnURL", $this->route->action("view", array("id" => $id,)));
        $this->assign("action", $this->route->action("update"));
        $this->assign("linea", $linea);

        $this->display("edit");
    }

    function update() {
        if ($this->request->submitted()) {
            $id = $this->request->get("id");
            $linea = $this->components->Lineas->getById($id);

            $linea->setAbreviatura($this->request->post("abreviatura"));
            $linea->setNombre($this->request->post("nombre"));

            $this->components->Lineas->update($linea);

            $this->view($id);
        } else {
            $this->edit();
        }
    }
    
    function create() {
        $linea = new Lineas();

        $this->assign("returnURL", $this->route->action("index"));
        $this->assign("action", $this->route->action("save"));
        $this->assign("linea", $linea);

        $this->display("create");
    }

    function save() {
        if ($this->request->submitted()) {
            $linea = new Lineas();

            $linea->setAbreviatura($this->request->post("abreviatura"));
            $linea->setNombre($this->request->post("nombre"));

            /* @var $newLinea Lineas */
            $newLinea = $this->components->Lineas->create($linea);

            $this->view($newLinea->getIdLinea());
        } else {
            $this->create();
        }
    }

    function remove() {
        $id = $this->request->get("id");
        $this->components->Lineas->remove($id);

        $this->index();
    }

}

?>